// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Fluent;
using Elastic.Clients.Elasticsearch.Requests;
using Elastic.Clients.Elasticsearch.Serialization;
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.QueryRuleset;

public sealed partial class PutQueryRulesetRequestParameters : RequestParameters
{
}

/// <summary>
/// <para>Creates or updates a query ruleset.</para>
/// </summary>
public sealed partial class PutQueryRulesetRequest : PlainRequest<PutQueryRulesetRequestParameters>
{
	public PutQueryRulesetRequest(Elastic.Clients.Elasticsearch.Id rulesetId) : base(r => r.Required("ruleset_id", rulesetId))
	{
	}

	internal override ApiUrls ApiUrls => ApiUrlLookup.QueryRulesetPut;

	protected override HttpMethod StaticHttpMethod => HttpMethod.PUT;

	internal override bool SupportsBody => true;

	internal override string OperationName => "query_ruleset.put";

	[JsonInclude, JsonPropertyName("rules")]
	public ICollection<Elastic.Clients.Elasticsearch.QueryRuleset.QueryRule> Rules { get; set; }
}

/// <summary>
/// <para>Creates or updates a query ruleset.</para>
/// </summary>
public sealed partial class PutQueryRulesetRequestDescriptor : RequestDescriptor<PutQueryRulesetRequestDescriptor, PutQueryRulesetRequestParameters>
{
	internal PutQueryRulesetRequestDescriptor(Action<PutQueryRulesetRequestDescriptor> configure) => configure.Invoke(this);

	public PutQueryRulesetRequestDescriptor(Elastic.Clients.Elasticsearch.Id rulesetId) : base(r => r.Required("ruleset_id", rulesetId))
	{
	}

	internal override ApiUrls ApiUrls => ApiUrlLookup.QueryRulesetPut;

	protected override HttpMethod StaticHttpMethod => HttpMethod.PUT;

	internal override bool SupportsBody => true;

	internal override string OperationName => "query_ruleset.put";

	public PutQueryRulesetRequestDescriptor RulesetId(Elastic.Clients.Elasticsearch.Id rulesetId)
	{
		RouteValues.Required("ruleset_id", rulesetId);
		return Self;
	}

	private ICollection<Elastic.Clients.Elasticsearch.QueryRuleset.QueryRule> RulesValue { get; set; }
	private Elastic.Clients.Elasticsearch.QueryRuleset.QueryRuleDescriptor RulesDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.QueryRuleset.QueryRuleDescriptor> RulesDescriptorAction { get; set; }
	private Action<Elastic.Clients.Elasticsearch.QueryRuleset.QueryRuleDescriptor>[] RulesDescriptorActions { get; set; }

	public PutQueryRulesetRequestDescriptor Rules(ICollection<Elastic.Clients.Elasticsearch.QueryRuleset.QueryRule> rules)
	{
		RulesDescriptor = null;
		RulesDescriptorAction = null;
		RulesDescriptorActions = null;
		RulesValue = rules;
		return Self;
	}

	public PutQueryRulesetRequestDescriptor Rules(Elastic.Clients.Elasticsearch.QueryRuleset.QueryRuleDescriptor descriptor)
	{
		RulesValue = null;
		RulesDescriptorAction = null;
		RulesDescriptorActions = null;
		RulesDescriptor = descriptor;
		return Self;
	}

	public PutQueryRulesetRequestDescriptor Rules(Action<Elastic.Clients.Elasticsearch.QueryRuleset.QueryRuleDescriptor> configure)
	{
		RulesValue = null;
		RulesDescriptor = null;
		RulesDescriptorActions = null;
		RulesDescriptorAction = configure;
		return Self;
	}

	public PutQueryRulesetRequestDescriptor Rules(params Action<Elastic.Clients.Elasticsearch.QueryRuleset.QueryRuleDescriptor>[] configure)
	{
		RulesValue = null;
		RulesDescriptor = null;
		RulesDescriptorAction = null;
		RulesDescriptorActions = configure;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (RulesDescriptor is not null)
		{
			writer.WritePropertyName("rules");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, RulesDescriptor, options);
			writer.WriteEndArray();
		}
		else if (RulesDescriptorAction is not null)
		{
			writer.WritePropertyName("rules");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.QueryRuleset.QueryRuleDescriptor(RulesDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (RulesDescriptorActions is not null)
		{
			writer.WritePropertyName("rules");
			writer.WriteStartArray();
			foreach (var action in RulesDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.QueryRuleset.QueryRuleDescriptor(action), options);
			}

			writer.WriteEndArray();
		}
		else
		{
			writer.WritePropertyName("rules");
			JsonSerializer.Serialize(writer, RulesValue, options);
		}

		writer.WriteEndObject();
	}
}